package com.example.leero.graduation.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.leero.graduation.API;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.GsonUtil;
import com.example.leero.graduation.Util.ImageLoader;
import com.example.leero.graduation.Util.StringUtil;
import com.example.leero.graduation.model.ApplyItem;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

/**
 * Created by Leero on 2017-5-27.
 */

public class ManageAdapter extends CommonAdapter<ApplyItem> {
    private Context mContext;
    private TextView tvAgree, tvReject, tvResult;
    private List<ApplyItem> list;
    private int actId;

    public ManageAdapter(Context context, int layoutId, List<ApplyItem> datas) {
        super(context, layoutId, datas);
        this.mContext = context;
        this.list = datas;
        this.actId = actId;
    }

    @Override
    protected void convert(ViewHolder holder, final ApplyItem applyItem, final int position) {
        tvAgree = holder.getView(R.id.tv_agree);
        tvReject = holder.getView(R.id.tv_reject);
        tvResult = holder.getView(R.id.tv_result);

        ImageView avatar = holder.getView(R.id.iv_avatar);
        if (!StringUtil.isEmpty(applyItem.avatar)) {
            ImageLoader.load(mContext, avatar, applyItem.avatar);
        } else {
            avatar.setImageResource(R.drawable.default_avatar_cricle);
        }

        // 0:女  1:男
        ImageView sex = holder.getView(R.id.iv_sex);
        if (applyItem.sex == 0) {
            sex.setImageResource(R.drawable.draw_women);
        } else {
            sex.setImageResource(R.drawable.draw_man);
        }

        setState(applyItem.state);
        holder.setText(R.id.tv_nickname, applyItem.nickname);

        tvAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(mContext)
                        .setMessage("确定同意用户"+applyItem.nickname+"的活动申请")
                        .setPositiveButton("同意", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                agreeApply(applyItem.act_id, applyItem.user_id, position);
                            }
                        })
                        .setNegativeButton("取消", null).show();
            }
        });
        tvReject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(mContext)
                        .setMessage("确定拒绝用户"+applyItem.nickname+"的活动申请")
                        .setPositiveButton("拒绝", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                rejectApply(applyItem.act_id, applyItem.user_id, position);
                            }
                        })
                        .setNegativeButton("取消", null).show();
            }
        });
    }

    // 同意报名处理
    private void agreeApply(int actID, int userId, final int position) {
        OkHttpUtils.post().url(API.AGREE_APPLY)
                .addParams("act_id", actID+"")
                .addParams("user_id", userId+"")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
                list.get(position).state = 2;
                notifyDataSetChanged();
            }
        });
    }

    // 拒绝报名申请
    private void rejectApply(int actID, int userId, final int position) {
        OkHttpUtils.post().url(API.REJECT_APPLY)
                .addParams("act_id", actID+"")
                .addParams("user_id", userId+"")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
                list.get(position).state = 4;
                notifyDataSetChanged();
            }
        });
    }

    private void setState(int state) {
        if (state == 1) {
            tvAgree.setVisibility(View.VISIBLE);
            tvReject.setVisibility(View.VISIBLE);
            tvResult.setVisibility(View.GONE);
        } else if (state == 2) {
            tvAgree.setVisibility(View.GONE);
            tvReject.setVisibility(View.GONE);
            tvResult.setVisibility(View.VISIBLE);
            tvResult.setText("已通过");
        } else if (state == 4) {
            tvAgree.setVisibility(View.GONE);
            tvReject.setVisibility(View.GONE);
            tvResult.setVisibility(View.VISIBLE);
            tvResult.setText("已拒绝");
        }
    }
}
